﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using PropertyManagment.common.Filter;
using PropertyManagment.common.lang;
using PropertyManagment.controllers;
using PropertyManagment.entity;
using PropertyManagment.mapper;
using PropertyManagment.service;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace PropertyManagment.controllers {
	public class TestController : Controller {

		private readonly ILogger<TestController> logger;

		private readonly SysUserService sysUserService;
		private readonly SysRoleService sysRoleService;
		private readonly SysMenuService sysMenuService;
		public SysMenuMapper sysMenuMapper { get; set; }

		public TestController(ILogger<TestController> logger, SysUserService sysUserService, SysRoleService sysRoleService, SysMenuService sysMenuService) {
			this.logger = logger;
			this.sysUserService = sysUserService;
			this.sysRoleService = sysRoleService;
			this.sysMenuService = sysMenuService;
		}

		[HttpGet]
		public Result error(String username) {
			throw new UserLoginException("用户名或者密码不正确!");
		}

		[HttpGet]
		public Result GetAllUser() {
			logger.LogError("111");
			var model = sysUserService.getAll();
			return Result.succ(model);
		}

		[HttpPost]
		public Result Get(int id) {
			logger.LogError("222");
			logger.LogError(id.ToString());
			var model = sysUserService.getById(id);
			return Result.succ(model);
		}

		[HttpPost]
		public Result Add(SysUser sysUser) {
			logger.LogError("333");

			//判断模型的值是否符合要求
			if (ModelState.IsValid) {
				//执行正常的业务逻辑
				logger.LogError(sysUser.ToString());
				var model = sysUserService.add(sysUser);
				return Result.succ(model);
			} else {
				//验证没通过，返回错误信息
				return Result.fail("用户名或密码不能为空");
			}
		}

		[HttpPost]
		public Result Delete(int id) {
			logger.LogError("444");
			logger.LogError(id.ToString());
			var model = sysUserService.delete(id);
			return Result.succ(model);
		}

		[HttpPost]
		public Result Update(SysUser updateUser) {
			logger.LogError("555");
			logger.LogError(updateUser.ToString());
			var model = sysUserService.update(updateUser);
			return Result.succ(model);
		}

		[HttpPost]
		public Result Test(int id) {
			logger.LogError("666");
            //logger.LogError(username);
            //var model = _sysUserService.getByUsername(username);

            Debug.WriteLine(HttpContext.Request.Form["username"]);

			//var model = sysUserService.getAll();
			var model = sysMenuMapper.getMenuIdByUserId(id);

			//_sysUserService.clearUserAuthorityInfoByRoleId(id);
			//_sysUserService.clearUserAuthorityInfoByMenuId(id);

			//var model = sysUserService.getAll();
			//var model = sysMenuService.tree();

			return Result.succ(model);
		}

	}
}
